% Notes for Hacker Within Software Carpentry Bootcamp, Day 2
% January 13, 2009

\documentclass{article}
\usepackage[usenames]{color}
\usepackage[
naturalnames = true, 
colorlinks = true, 
linkcolor = Black,
anchorcolor = Black,
citecolor = Black,
menucolor = Black,
urlcolor = Blue
]{hyperref}

\title{Software Carpentry Bootcamp}
\author{The Hacker Within}

\setlength{\parindent}{0em}

\begin{document}
%\maketitle

\section{The shell is a programming language}
Programming languages usually have many of the same features.
\begin{enumerate}
	\item collect many operations into single entities
	\item input and output
	\item variables and state
	\item irritating syntax
	\item special characters
\end{enumerate}

Additionally there are more shells than you really care to learn.  bash is most widely used, tcsh and ksh are also popular.  Since cae defaults to tcsh, that's what we'll use.

\begin{tabular}{|l|l|l|}
	\hline
	Shell & Name & Description \\
	\hline
	sh & Bourne shell & popular, ubiquitous shell in 1977, still guaranteed on all Unixes \\
	csh & C shell & improves on sh \\
	ksh & Korn shell & backwards compatable with sh, but extends and borrows from other shells \\
	bash & Bourne again shell & free software replacement to sh, much evolved since origins \\
	tcsh & Tenex C shell & updated and extended C shell \\
	\hline
\end{tabular}


\section{Alias}
You can alias commands to other commands.  Alias does a simple replacement of the first argument by the second.
\begin{verbatim}
	alias ls 'ls --color'
	alias ll 'ls -l'
\end{verbatim}

There are a bunch of terminal programs.  xterm is probably the lightest weight of them, so I tend to use it when opening terminals over a ssh connection.
\begin{verbatim}
	alias xterm 'xterm &'
\end{verbatim}

\section{Environment Variables}
New program ``echo''
\begin{verbatim}
	echo "Hi mom!"
	echo $USERNAME
	echo $PWD
\end{verbatim}

Shells variables are replaced with their values when executed.  You can create your own and change existing variables with the setenv command. (tcsh only!)  Variables are case sensitive and are invoked by prepending a \$ to the variable name.
\begin{verbatim}
	setenv MYnewVARY "hi mom"
	echo $MYnewVARY
	echo MYnewVARY
	echo MYNEWVARY
	echo $MYNEWVARY
\end{verbatim}

Commonly/Occasionally useful shell variables:

\begin{tabular}{|l|l|}
	\hline
	Variable name & Meaning \\
	\hline
	USER & User name \\
	PATH & List of absolute paths that searched for executables \\
	PWD & Current directory, short for Print Working Directory \\
	EDITOR & Default text editor \\
	GROUP & Groups the user belongs too \\
	HOME & home directory \\
	~ & same as \$HOME \\
	DISPLAY & used in forwarding graphics over a network connection \\
	LD\_LIBRARY\_PATH & Like \$PATH, but for precompiled libraries \\
	FC & Fortran compiler \\
	CC & C compiler \\
	\hline
\end{tabular}

Non-free software will frequently make you assign a shell variable to point to the licensing server.

In tcsh ``:'' has special meaning.  To append to your \$PATH you must ``escape'' the character.  That means to precede the special character by a backslash (\\).  The backslash tells the shell that the following character is to be interpreted as a text character rather than a special character.
\begin{verbatim}
	setenv PATH $PATH\:/some/new/path/to/append
\end{verbatim}

Most nonalphanumeric characters have special meaning.  You should stick to ``.'', ``\_'', ``-'', ``='' and alpha numeric characters for filenames.

exercise: cd in to an irritiating directory

\section{Text Editors}
A ``text editor'' edits text files.  This is fundamentally different from MS Word or other WYSIWYG word processors.  The most typical use is to edit source code files.  We're going to try to expand your notion of what is a ``source code'' file.

For this class we're going to use the text editor nano because it is very, very easy to use.  To open a file (existant or not):
\begin{verbatim}
	nano text_file
\end{verbatim}

A buffer (loosely) is temporary storage for data.  In the context of nano, it is the thing you edit and then save to a file.  Very powerful text editors exist, vim and emacs being the most popular.  The debate over which one is better is one of the great computing Holy Wars.  Learn the one your friends use, preferably vim.

Excersise:
create a text file with whatever name you see fit.  
Put some text into it.  
Close it.
Open it back up and make it at least 11 lines long.
Close it.
Look at it with less.
Look at it with head.
Look at it with tail.

\section{Config files}
Hopefully you've been convinced that the shell is a very customizable environment, however manually setting up your environment everytime you open a shell is onerous.  To solve this problem shells (and many other programs have) configuration files that modify the program.  tcsh by default executes the file ~/.tcshrc  Additionally, the command:
\begin{verbatim}
	source rc_file
\end{verbatim}
will execute an rc file as if it were .tcshrc

\section{Redirection}
You can string together many commands/programs to accomplish things never intened or expected by the system designers.  There are 4 common means to redirection, but mostly you will use $|$.

\begin{tabular}{|l|l|l|}
	\hline
	Symbol & Usage & Example \\
	\hline
	$|$ & Directs stdout of first command to stdin in second & \verb,ls | less, \\
	` ` & (Tick/Backquote) Evaulates commands inside and replaces & 
	\verb,wc ` find $HOME/bootcamp/tuesday/ -name 'Make*' `, \\
	xargs & Works just like ticks, but is nestable &
	\verb,find $HOME/bootcamp/tuesday/ -name 'Make*' | xargs wc,\\
	$>$ & Writes stdout to a file, overwrites if already exists & \verb,ls > tmp.txt, \\
	$>>$ & Appends stdout to a file & \verb,ls >> tmp.txt, \\
	\hline
\end{tabular}

From shell directory

\begin{verbatim}
	cat Makefile thriller.txt notes.txt | wc > Make_thrill_tex_count.txt
\end{verbatim}

\section{Basic scripts}
The shell is actually a scripting language.  You can gather a bunch of shell commands into a file and execute them as if it were a program.  Ther first line is called the ``shebang.''  It identifies what program should execute the script.  Alternatively, you can call the program by name and pass the name of the script file.

Open up simple.tcsh.

\end{document}
